var preClass = 'imgNew1Obj_ '
function newImg1(className, options) {
  var imgNew1Obj = {
    slideActiveIndex: 1, // 轮播图下标  从1开始
    slides: $('.' + className + ' .slide-item'), // 轮播图item  dom集合
    dots: $('.' + className + ' .dot-item'), // 图例item

    timer: undefined, // 轮播图 计时器
    options: options // 传入的其他事件
  }
  window[preClass + className] = imgNew1Obj

  showSlides(imgNew1Obj, imgNew1Obj.slideActiveIndex)

  // 图例点击切换图片事件
  imgNew1Obj.dots.click(function () {
    var index = $(this).index()
    showSlides(imgNew1Obj, index)
    console.log('dots 图例点击事件')
  })
  // 图例移入切换  暂停图片事件
  imgNew1Obj.dots.mouseenter(function () {
    console.log(111)
    var index = $(this).index()
    showSlides(imgNew1Obj, index)
    stop(imgNew1Obj)

    console.log('dots 图例移入事件')
  })

  // 图例移出继续图片事件
  imgNew1Obj.dots.mouseleave(function () {
    run(imgNew1Obj)
    console.log('dots 图例移出事件')
  })
  imgNew1Obj.slides.click(function () {
    console.log('图片点击事件')
    if (imgNew1Obj.options && imgNew1Obj.options.onclick) {
      imgNew1Obj.options.clickImg(this, imgNew1Obj)
    }
  })
}
function showSlides(imgNew1Obj, index) {
  imgNew1Obj.slideActiveIndex = index

  if (index > imgNew1Obj.slides.length) {
    imgNew1Obj.slideActiveIndex = 1
  }

  if (index < 1) {
    imgNew1Obj.slideActiveIndex = imgNew1Obj.slides.length
  }

  // $方法 移除active
  imgNew1Obj.slides.removeClass('active')
  imgNew1Obj.dots.removeClass('active')

  // $方法 添加active
  $(imgNew1Obj.slides[imgNew1Obj.slideActiveIndex - 1]).addClass('active')
  $(imgNew1Obj.dots[imgNew1Obj.slideActiveIndex - 1]).addClass('active')

  for (var index = 0; index < imgNew1Obj.dots.length; index++) {
    if($(imgNew1Obj.dots[index]).hasClass('active')) {
      $(imgNew1Obj.dots[index]).children()[0].src='./img/'+(index+1)+'-2.gif';
    } else {
      $(imgNew1Obj.dots[index]).children()[0].src='./img/'+(index+1)+'.gif';
    }  
  }
  
  // 计时 5s 后下一张图片
  clearTimeout(imgNew1Obj.timer)
  imgNew1Obj.timer = setTimeout(function () {
    showSlides(imgNew1Obj, imgNew1Obj.slideActiveIndex + 1)
  }, 5000)
}
function stop(imgNew1Obj) {
  clearTimeout(imgNew1Obj.timer)
}

function run(imgNew1Obj) {
  imgNew1Obj.timer = setTimeout(function () {
    showSlides(imgNew1Obj, imgNew1Obj.slideActiveIndex + 1)
  }, 5000)
}
